<?php
/**
 * 层级管理
 * Created by PhpStorm.
 * @User: 王海东
 * @Date: 2018/12/26 10:43
 */

namespace app\adm\controller;

use app\common\model\Admin;
use app\common\model\Hierarchy;

class HierarchyController extends BaseController
{
    /**
     * 层级列表
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
   public function lists()
   {
       $a       = new Admin();
       $info    = $a->info(UID);

       $h    = new Hierarchy();
       $data = $h->listHierarchy($info['hierarchy_id']);
       rjd($data);
   }

    /**
     * 添加层级
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
   public function add()
   {
       $in = $this->I([
           [
               "title|层级名称",
               null,
               "s",
               "require",
           ],
           [
               "pid|上级id",
               null,
               "d"
           ]
       ]);

       $h = new Hierarchy();

       if(!$h->addHierarchy($in['title'],$in['pid'])) {
           rj(1,'添加失败');
       }

       rj();
   }

    /**
     * 编辑层级
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
   public function edit()
   {
       $in = $this->I([
           [
               "title|层级名称",
               null,
               "s",
               "require",
           ],
           [
               "id|层级id",
               null,
               "d",
               "require"
           ]
       ]);

       $h = new Hierarchy();

       if(!$h->editHierarchy($in['title'],$in['id'])) {
           rj(1,'修改失败');
       }

       rj();
   }

    /**
     * 删除层级
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
   public function delete()
   {
       $in = $this->I([
           [
               "id|层级id",
               null,
               "d",
               "require"
           ]
       ]);

       $h = new Hierarchy();

       $data = $h->listHierarchy($in['id'],true);

       if($data)
           rj(1,'该目录还有子目录，不能删除');

       if(!$h->delHierarchy($in['id'])) {
           rj(1,'删除失败');
       }

       rj();
   }
}